999C - Alphabetic Removals - CodeForces Solution


implementation *1200

Please click on ads to support us..

Python Code:

n,k = map(int,input().strip().split())
s = input()
l = n
i=97
while k!=0 and i<123:
	c = chr(i)
	s = s.replace(c,'',k)
	nl = len(s)
	k -= (l-nl)
	l = nl
	i+=1
print(s)

C++ Code:

//This is coded by NISHANTH VM
//#include <bits/stdc++.h>
#include <iostream> 
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
typedef map<int,int> mii;
typedef map<int,v32> miv;
ll MOD = 998244353;
double eps = 1e-12;
#define rep(i,e) for(ll i = 0; i < e; i++)
#define repFR(i,s,e) for(ll i = s; i < e; i++)
#define repR(i,s) for(ll i = s; i >= 0; i--)
#define repRR(i,s,e) for(ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define INF 2e18
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
 

void solve(){
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n,k;
cin>>n>>k;
string s;
cin>>s;
map<char,int> m;
for(char a:s){
    m[a]++;
}
for(auto i:m){
    if(k>0){
        if(i.second <= k ){
        k = k - i.second;
        for(int j=0;j<n;j++){
            if(s[j]==i.first) s[j] = 'A';
        }
        }
        else{
            int ref1 = k ;
            k = k - i.second;
            for(int j=0;j<n;j++){
                if(s[j]==i.first && ref1!=0){
                    s[j]='A';
                    ref1--;
                }
                if(ref1==0){
                    break;
                }              
            } 
        }
    }
    else{
        break;
    }
}
    for(char a:s){
        if(a!='A') cout<<a;
    }
    cout<<"\n";
}
int main()
{
fast_cin();
ll t;
// cin >> t;
for(int it=1;it<=1;it++) {
 //cout << "Case #" << it+1 << ": ";
 solve();
 }
 return 0;
}


Comments

Submit
0 Comments
More Questions

1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines
328. Odd Even Linked List
1219. Path with Maximum Gold
1268. Search Suggestions System
841. Keys and Rooms
152. Maximum Product Subarray
337. House Robber III
869. Reordered Power of 2
1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank